/**
 * Created by Administrator on 2015/4/29.
 */
var model = require('../model/model').model;
var dao = require('../model/dao').PromiseDao;
var Promise = require('promise');
module.exports = {
    index: function (req, res) {
        res.render('article', {});
    },
    detail: function (req, res, id) {
        dao.findOne(model.ArticleModel, {id: id}).done(function (doc) {
            res.render('article', {article: doc});
        });

    },
    /**
     * 保存
     * @param req
     * @param res
     */
    post: function (req, res) {
        var params = req.body;
        var promiseArr = [dao.findOne(model.CatalogModel, {name: params.catalog})];
        var tagArr = [].concat(params.tags);
        var noCatalog = false;
        tagArr.forEach(function (n) {
            promiseArr.push(dao.findOne(model.TagModel, {name: n}));
        });

        Promise.all(promiseArr)
            .then(function (argArr) {
                promiseArr = [dao.findOne(model.UserModel, {id: req.session.user.id})];
                params.tags = [];
                argArr.forEach(function (n, i) {
                    if (n) {
                        var doc = {id: n.id, name: n.name};
                        if (i === 0) {
                            params.catalog = doc;
                        } else {
                            params.tags.push(doc);
                        }
                    } else {
                        if (i === 0) {
                            noCatalog = true;
                            promiseArr.push(dao.create(model.CatalogModel, {name: params.catalog}));
                        } else {
                            promiseArr.push(dao.create(model.TagModel, {name: tagArr[i - 1]}));
                        }

                    }
                });
            }, function () {//失败
                // console.log(arguments);
            }).then(function () {
                Promise.all(promiseArr)
                    .then(function (argArr) {
                        argArr.forEach(function (n, i) {
                            var doc = {id: n.id, name: n.name};
                            if (i === 0) {
                                params.author = {id: n.id, username: n.username};
                            } else if (noCatalog) {
                                params.catalog = doc;
                            } else {
                                params.tags.push(doc);
                            }
                        });
                    }).then(function () {
                        console.log(params);
                        var Article = new model.ArticleModel({
                            author: params.author,
                            date: new Date(),
                            title: params.title,
                            content: params.content,
                            catalog: params.catalog,
                            tags: params.tags
                        });
                        dao.save(Article).done(function () {
                            console.log(arguments);
                            res.json({ret: 1});
                        });
                    });
            });


    },
    findPage: function (req, res, param) {
        dao.find(model.ArticleModel, {}, []).then(function (doc) {

        });
    },
    /**
     * 查询标签
     * @param req
     * @param res
     * @param query
     */
    queryTag: function (req, res, query) {
        dao.find(model.TagModel, {name: new RegExp(query, 'i')}).then(function(doc){
            res.json(doc.map(function(n){
                return n.name;
            })).end();
        });
    },
    queryCatalog: function (req, res, query) {
        dao.find(model.CatalogModel, {name: new RegExp(query, 'i')}).then(function(doc){
            res.json(doc.map(function(n){
                return n.name;
            })).end();
        });
    }
};
